package com.live.proxy.sql;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.live.proxy.model.UserModel;


public class UserDatabase {
	private static MyDatabaseHelper helper;
	public UserDatabase(Context context){
		//创建数据库和表
		helper = new MyDatabaseHelper(context, "user.db",null,3,null);
	}
	@SuppressLint("Range")
	public static UserModel select(){
		UserModel model= new UserModel();
		SQLiteDatabase database = helper.getReadableDatabase();
		Cursor cursor = database.rawQuery("select * from user_info where id = 1", null);
		if (cursor != null && cursor.getCount() > 0) {
			if (cursor.moveToNext()) {
				model.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
				model.setAccount(cursor.getString(cursor.getColumnIndex("account")));
				model.setPassword(cursor.getString(cursor.getColumnIndex("password")));
				model.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
				model.setToken(cursor.getString(cursor.getColumnIndex("token")));
				model.setAppId(cursor.getInt(cursor.getColumnIndex("app_id")));
				model.setAppName(cursor.getString(cursor.getColumnIndex("app_name")));
				model.setAutoType(cursor.getInt(cursor.getColumnIndex("auto_type")));
				model.setDirectType(cursor.getInt(cursor.getColumnIndex("direct_type")));
				model.setInteYue(cursor.getInt(cursor.getColumnIndex("inte_yue")));
			}
			cursor.close();
		}
		database.close();
		return model;
	}
	//恢复用户状态
	public static void restoreStatus() {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET status=1 WHERE id = 1");
		database.close();
	}
	//清除token
	public static void clearToken(int status) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET token = '',status="+status+" WHERE id = 1");
		database.close();
	}
	//更改昵称
	public static void updateNickname(String nickname) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET nickname='"+nickname+"' WHERE id = 1");
		database.close();
	}
	//更改密码
	public static void updatePass(String pass) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET password='"+pass+"' WHERE id = 1");
		database.close();
	}
	//更改积分
	public static void updateInteYue(int inte) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE user_info SET inte_yue="+inte+" WHERE id = 1");
		database.close();
	}
	//更改登录账号
	@SuppressLint("Range")
	public static void updateUserData(UserModel model) {
		SQLiteDatabase database = helper.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("app_id", model.getAppId());
		values.put("app_name", model.getAppName());
		values.put("nickname", model.getNickname());
		values.put("account", model.getAccount());
		values.put("password", model.getPassword());
		values.put("token", model.getToken());
		values.put("status", 1);
		values.put("auto_type", model.getAutoType());
		values.put("direct_type", model.getDirectType());
		values.put("inte_yue", model.getInteYue());
		database.update("user_info", values, "id = ?", new String[] {String.valueOf(1)});
		values.clear();
		database.close();
	}
	//新增一条
	public static void insert(UserModel model) {
		SQLiteDatabase database = helper.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("app_id", model.getAppId());
		values.put("app_name", model.getAppName());
		values.put("nickname", model.getNickname());
		values.put("account", model.getAccount());
		values.put("password", model.getPassword());
		values.put("token", model.getToken());
		values.put("status", 1);
		values.put("auto_type", model.getAutoType());
		values.put("direct_type", model.getDirectType());
		values.put("inte_yue", model.getInteYue());
		database.insert("user_info", null, values); // 插入一条数据
		values.clear();
		database.close();
    }
	
}
